import requests
import json
import re
from lxml import etree
jd_id=[]
def Url_jd(sum):
    url = 'https://search.jd.com/Search?keyword=%E7%AC%94%E8%AE%B0%E6%9C%AC%E7%94%B5%E8%84%91&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&suggest=3.def.0.V12--12s0%2C20s0%2C38s0%2C97s0&wq=%E7%AC%94%E8%AE%B0%E6%9C%AC&page='+str(sum)
    headers = {
        'cookie': 'shshshfpa=eedb8aaa-b73d-2e28-b600-af975b74d377-1564496231; qrsc=3; shshshfpb=hLyq2HDYROsAsIJryO6fQkw%3D%3D; __jdu=15644961912871758872849; xtest=6546.cf6b6759; unpl=V2_ZzNtbUNeExV3C0IAeB4PBWJTEQ4RAhRHdggSUCwfX1IyURANclRCFX0URldnGVQUZwEZWUdcRxJFCEdkeB5fA2AFEFlBZxBFLV0CFi9JH1c%2bbRpeQ1FCF3YAR1R9KWwGZzMSXHJXRxJwDU5TexFcBm4FEltGXkoTdgFDXEspWzVXMxJVS1VKHEUJdlVLWwhZYwoVWkBWDhVxD0NRcx5cDWcAG1tCUUccfA5FXX4RbARXAA%3d%3d; rkv=V0200; areaId=13; ipLoc-djd=13-1072-2974-0; __jdv=76161171|baidu|-|organic|not set|1578452137453; __jda=122270672.15644961912871758872849.1564496191.1578449542.1578452137.39; __jdc=122270672; 3AB9D23F7A4B3C9B=YG35FVLRMX753SL7XXOW2LXD37LVJ334EOK5ZJRIUI4TYAVRADMJEPLUPIS2ZGURBNISLET5HKQLBDE72ZGZGIFWZM; shshshfp=3b859d84f8b86e95dcdceb3726b2f9f2; __jdb=122270672.8.15644961912871758872849|39.1578452137; shshshsID=19a8e1bf257d8a8104bd94e51c7a7c54_8_1578452838934'
    ,'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0'
    }
    r = requests.get(url,headers=headers)
    r.encoding='utf-8'
    html = etree.HTML(r.text)
    con = html.xpath('//*[@id="J_goodsList"]/ul/li/@data-sku')
    for i in con:
        jd_id.append(i)

def analysis(num):
    try:
        with open('电脑价格表.csv','a') as f:

            print(num)
            url = 'https://item.jd.com/'+str(num)+'.html'
            headers = {
                'cookie': 'shshshfpa=eedb8aaa-b73d-2e28-b600-af975b74d377-1564496231; qrsc=3; shshshfpb=hLyq2HDYROsAsIJryO6fQkw%3D%3D; __jdu=15644961912871758872849; xtest=6546.cf6b6759; unpl=V2_ZzNtbUNeExV3C0IAeB4PBWJTEQ4RAhRHdggSUCwfX1IyURANclRCFX0URldnGVQUZwEZWUdcRxJFCEdkeB5fA2AFEFlBZxBFLV0CFi9JH1c%2bbRpeQ1FCF3YAR1R9KWwGZzMSXHJXRxJwDU5TexFcBm4FEltGXkoTdgFDXEspWzVXMxJVS1VKHEUJdlVLWwhZYwoVWkBWDhVxD0NRcx5cDWcAG1tCUUccfA5FXX4RbARXAA%3d%3d; rkv=V0200; areaId=13; ipLoc-djd=13-1072-2974-0; __jdv=76161171|baidu|-|organic|not set|1578452137453; __jda=122270672.15644961912871758872849.1564496191.1578449542.1578452137.39; __jdc=122270672; 3AB9D23F7A4B3C9B=YG35FVLRMX753SL7XXOW2LXD37LVJ334EOK5ZJRIUI4TYAVRADMJEPLUPIS2ZGURBNISLET5HKQLBDE72ZGZGIFWZM; shshshfp=3b859d84f8b86e95dcdceb3726b2f9f2; __jdb=122270672.8.15644961912871758872849|39.1578452137; shshshsID=19a8e1bf257d8a8104bd94e51c7a7c54_8_1578452838934'
            ,'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0'
            }
            h = {
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0'
            }
            jg = 'https://p.3.cn/prices/mgets?skuIds=J_'+str(num)
            r1 = requests.get(url=jg,headers=h)
            jd = json.loads(r1.text)
            for i in jd:
               dn_jg = str(i.get('p'))
            r = requests.get(url,headers=headers)
            name =re.findall('商品名称：(.*?)</li>',r.text)
            memory = re.findall('内存容量：(.*?)</li>',r.text)
            cpu = re.findall('处理器：(.*?)</li>',r.text)
            storage = re.findall('硬盘容量：(.*?)</li>', r.text)
            size = re.findall('屏幕尺寸：(.*?)</li>', r.text)
            model = re.findall('显卡型号：(.*?)</li>', r.text)

            #pinpai = re.findall("target='_blank'>(.*?)</a>",r.text)[0]
            html = etree.HTML(r.text)
            pinpai = html.xpath('//*[@id="parameter-brand"]/li/a/text()')[0]
            con = str.format("{},{},{},{},{},{},{},{},{}\n",pinpai,"".join(name),"".join(memory),"".join(cpu),"".join(storage),"".join(size),"".join(model),dn_jg,num)
            f.write(con)
            print(con)
    except:
        print('错误')
if __name__ == '__main__':
    for i in range(1,200):
        if i%2 == 1:
            Url_jd(i)
    for x in jd_id:
        analysis(x)
